package cn.mumzone.pos.presenter;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.util.MD5Util;
import cn.mumzone.basic.util.PatternUtils;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.model.VerifyReturnAuthEntity;
import cn.mumzone.pos.model.source.UserRepository;
import cn.mumzone.pos.presenter.contract.UpReturnMoneyAuthContract;

/**
 * Created by qiuzhenhuan on 2019/1/9.
 */

public class UpReturnMoneyAuthPresenter extends AbsPresenter<UpReturnMoneyAuthContract.View, UserRepository>
        implements UpReturnMoneyAuthContract.Presenter {

    @Override
    public void verifyReturnAuth(String phone, String passWord) {

        if(!checkInput(phone,passWord)){
            return;
        }


        ApiCallBack<VerifyReturnAuthEntity> callBack = new ApiCallBack<VerifyReturnAuthEntity>() {
            @Override
            public void onSuccess(VerifyReturnAuthEntity entity) {
                mView.dismissLoadingDialog();
                if(!StringUtils.isEmpty(entity.getAccessType()) && entity.getAccessType().equals("1")){
                    mView.showAuthSuccess();
                }else {
                    mView.showError("授权失败");
                }
            }

            @Override
            public void onError(BaseResponse response) {
                mView.dismissLoadingDialog();
                mView.showError(response.getMsg());
            }
        };
        mView.showLoadingDialog();
        mRepo.verifyReturnAuth(AppConstants.TEMP_SELLER_ID,phone, MD5Util.md5ForStr(passWord),callBack);
    }

    /**
     * 检查输入内容
     *
     * @param phone
     * @param pwd
     * @return
     */
    public boolean checkInput(String phone, String pwd) {

        // 校验手机号
        if (!PatternUtils.isMobileNO(phone)) {
            mView.showError("请输入正确的手机号");
            return false;
        }

        //校检登录密码格式
        if (!PatternUtils.checkPassWord(pwd)) {
            mView.showError("密码格式不正确");
            return false;
        }

        //校检登录密码6-20
        if (!PatternUtils.checkPassWordLength(pwd)) {
            mView.showError("请输入6-20位的登录密码");
            return false;
        }

        return true;
    }
}
